/*
 * INIParser.h
 *
 *  Created on: Apr 29, 2011
 *      Author: burek
 */

#ifndef INIPARSER_H_
#define INIPARSER_H_

#include "SMap.h"
#include "KeyValuePair.h"

struct ByteStream;

class INIGroup
{
private:
	char * title;
	SMap<KeyValuePair> vals;

public:
	INIGroup();
	~INIGroup();

	SMap<KeyValuePair> & getVals();
	void setTitle(const char * k);
	const char * getTitle() const;
	int getSize() const;
	void putKVP(KeyValuePair * kvp);
	KeyValuePair * getKey(const char * key);
	const char * getValue(const char * key);
	KeyValuePair * touchKey(const char * key);
	void setValue(const char * key, const char * value);
};

class INIParser
{
private:
	SMap<INIGroup> groups;
	char lastFileName[128];
	bool saveOnExit;

public:
	INIParser();
	unsigned int calcLength();
	int getGroupCount() const;
	INIGroup * getGroupAt(int i);
	ByteStream * saveToBuffer(ByteStream * bs);
	bool saveToFile(const char * fileName);
	~INIParser();

	void setSaveOnExit(bool save);
	KeyValuePair * getKey(const char * group, const char * name);
	int getValueInt(const char * group, const char * name, int def);
	unsigned int getValueUXInt(const char * group, const char * name, unsigned int def);
	float getValueFloat(const char * group, const char * name, float def);
	const char * getValue(const char * group, const char * name);
	bool notNull(const char * group, const char * name);
	const char * getValueDef(const char * group, const char * name, const char * def);
	INIGroup * getGroup(const char * name);
	KeyValuePair * touchKey(const char * group, const char * name);
	void setValue(const char * group, const char * name, const char * value);
	void setValueInt(const char * group, const char * name, int value);
	void setValueFloat(const char * group, const char * name, float value);
	void setIfNotSet(const char * group, const char * name, const char * value);
	bool loadFromBuffer(ByteStream * bs);
	bool loadFromFile(const char * fileName);

	INIParser(const char * fileName);
	INIParser(ByteStream * buffer);
};

#endif /* INIPARSER_H_ */
